---
sidebar_position: 40
description: How to use the OpenFeature JAVA SDK
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# JAVA SDK usage

## Install dependencies

The first things we will do is install the **Open Feature SDK** and the **GO Feature Flag provider**.

<Tabs groupId="code">
  <TabItem value="maven" label="maven">

```xml
<dependency>
    <groupId>dev.openfeature</groupId>
    <artifactId>javasdk</artifactId>
    <version>[0,)</version>
</dependency>
<dependency>
    <groupId>dev.openfeature.contrib.providers</groupId>
    <artifactId>go-feature-flag</artifactId>
    <version>[0,)</version>
</dependency>
```

  </TabItem>
  <TabItem value="gradle" label="gradle">

```gradle
implementation group: 'dev.openfeature', name: 'javasdk', version: '0.+'
implementation group: 'dev.openfeature.contrib.providers', name: 'go-feature-flag', version: '0.+'
```

  </TabItem>
</Tabs>


## Initialize your Open Feature client
To evaluate the flags you need to have an Open Feature configured in you app.
This code block shows you how you can create a client that you can use in your application.

```java
import dev.openfeature.contrib.providers.gofeatureflag.*;
import dev.openfeature.contrib.providers.gofeatureflag.exception.*;
import dev.openfeature.sdk.*;

// ...

GoFeatureFlagProviderOptions options = GoFeatureFlagProviderOptions.builder().endpoint("http://localhost:1031/").build();
GoFeatureFlagProvider provider = new GoFeatureFlagProvider(options);

OpenFeatureAPI.getInstance().setProvider(provider);
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
Client featureFlagClient = api.getClient();
```

## Evaluate your flag

This code block explain how you can create an `EvaluationContext` and use it to evaluate your flag.

:::note
In this example we are evaluating a `boolean` flag, but other types are available.

**Refer to the [Open Feature documentation](https://docs.openfeature.dev/docs/reference/concepts/evaluation-api#basic-evaluation) to know more about it.**
:::

```java
// Context of your flag evaluation.
EvaluationContext userContext = new MutableContext()
        .add("firstname", "john")
        .add("lastname", "doe")
        .add("email","john.doe@gofeatureflag.org")
        .add("admin", true)
        .add("anonymous", false);
// With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
userContext.setTargetingKey("1d1b9238-2591-4a47-94cf-d2bc080892f1");



Boolean adminFlag = featureFlagClient.getBooleanValue("flag-only-for-admin", false, userContext);
if (adminFlag) {
    // flag "flag-only-for-admin" is true for the user
} else {
    // flag "flag-only-for-admin" is false for the user
}
```

## Contribute to the provider
You can find the source of the provider in the [`open-feature/java-sdk-contrib`](https://github.com/open-feature/java-sdk-contrib/tree/main/providers/go-feature-flag) repository.